package com.selenium.dinner;

import java.util.Iterator;
import java.util.Set;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriverCommandProcessor;
//import org.openqa.selenium.SeleneseCommandExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.thoughtworks.selenium.DefaultSelenium;
//import org.openqa.selenium.remote.CommandExecutor;
//import org.openqa.selenium.remote.DesiredCapabilities;
//import org.openqa.selenium.remote.RemoteWebDriver;


//import com.thoughtworks.selenium.DefaultSelenium;

import junit.framework.TestCase;

public class DinnerRatRev extends Abstract{

	private static final String SIGN_IN = "//div[contains(text(),'Sign In')]";
	private static final String OK_BUTTON = "//span[@role='button']";
	private static final String EMPLOYEE_ID = "//input[@placeholder='Employee ID']";
	String EID = "I0069";
	
//	WebDriver driver = new FirefoxDriver();
//	DefaultSelenium selenium = new DefaultSelenium(new WebDriverCommandProcessor("http://www.google.com", driver));
	//	DefaultSelenium browse = new DefaultSelenium("localhost", 4444, "*safari", "https://google.com/");
	//	CommandExecutor executor = new SeleneseCommandExecutor(browse);
	//	DesiredCapabilities dc = new DesiredCapabilities();
	//	WebDriver driver = new RemoteWebDriver(executor, dc);

	@Test
	public void testSignIn(DefaultSelenium selenium, WebDriver driver) throws Exception {

		//uncomment untuk dijalanin di computer ika
		//		driver.get("file:///Library/WebServer/Documents/Workspace/Maretska_Dinner_widget/trunk/assets/www/html/DinnerReview.html#dinnerReviewSignIn");
		
		//dicomment klo dijalanin di computer ika
		driver.get("http://localhost/Workspace_Trainee/Dinner_widget/trunk/assets/www/html/DinnerReview.html#dinnerReviewSignIn");
		
		Thread.sleep(3000);
		driver.manage().window().maximize();
		selenium.windowMaximize();
		Thread.sleep(5000);

		//Sign In Page
		System.out.println("bismillah");
		
		/* To scroll page with Javascripts Function */
		JavascriptExecutor jse = (JavascriptExecutor)driver;
		jse.executeScript("scroll(0, 250)");

		waitForElement(selenium, SIGN_IN);
		driver.findElement(By.xpath(SIGN_IN)).click();
		Thread.sleep(3000);
		System.out.println("bismillah2");

		//multiple window
		waitForText(3);
		Set<String> handels = driver.getWindowHandles();
		Iterator<String> it = handels.iterator();
		while (it.hasNext()){
			String parent = it.next();
			String newwin = it.next();

			driver.switchTo().window(newwin);

			Thread.sleep(1000);
			//invalid FB > tap cancel button
//			driver.findElement(By.xpath("//input[@value='Batal']")).click();
//			Thread.sleep(4000);
//			driver.findElement(By.xpath("//span[@class='first-button-alert-popup-custom alert-customize-main-button-ok']")).click();
//			Thread.sleep(5000);
		
			//valid FB
			driver.findElement(By.xpath("//input[@name='email']")).sendKeys("sysy8956@yahoo.com");
			Thread.sleep(1000);
			driver.findElement(By.xpath("//input[@class='inputpassword']")).click();
			Thread.sleep(1000);
			driver.findElement(By.xpath("//input[@class='inputpassword']")).sendKeys("sayang zaa selalu");
			Thread.sleep(1000);
			driver.findElement(By.xpath("//input[@name='login']")).click();
			Thread.sleep(3000);


			driver.switchTo().window(parent);
		}

		//Blank employeeId
		waitForElement(selenium, "//div[@class='dinner-button-label']");
		driver.findElement(By.xpath("//div[@class='dinner-button-label']")).click();
		Thread.sleep(2000);
		driver.findElement(By.xpath(OK_BUTTON)).click();
		Thread.sleep(2000);
		
		//Invalid employeeId
		driver.findElement(By.xpath(EMPLOYEE_ID)).sendKeys("i000");
		driver.findElement(By.xpath("//div[@class='dinner-button-label']")).click();
		waitForElement(selenium, OK_BUTTON);
		driver.findElement(By.xpath(OK_BUTTON)).click();
		waitForElement(selenium, EMPLOYEE_ID);	
		
		//Valid employeeId
		waitForElement(selenium, EMPLOYEE_ID);
		driver.findElement(By.xpath(EMPLOYEE_ID)).clear();
		waitForElement(selenium, EMPLOYEE_ID);
		driver.findElement(By.xpath(EMPLOYEE_ID)).sendKeys("I0069");
		waitForText(3);

		//button continue
		driver.findElement(By.xpath("//div[@class='dinner-button-label']")).click();
		waitForText(5);
	}
}
